*-------------------------------------------------------------------------------------------------------------*
* The Causal Effects of R&D Grants: Evidence from a Regression Discontinuity (Santoleri et al., 2020)
*-------------------------------------------------------------------------------------------------------------*

/// Table 7: SOE-winning firms vs rest of losing firms

use $data_path/DATA_ALL_FINAL.dta, clear

* compute dep vars in log levels
gen assets_post = ln(totalassets_tp1)
gen revenues_post = ln(opturnover_tp1)
gen employees_post = ln(numberofemployees_tp1)

foreach var of varlist assets_post revenues_post employees_post  {
		winsor2 `var', cuts(2 98) suff(_w)
}

* dependent var, treatment var, pre-treatment var
local dvar1 ln_citw_patents_post_w seal ln_citw_patents_pre_w
local dvar2 pe_post seal pe_pre
local dvar3 assets_post seal assets_pre_w
local dvar4 employees_post_w seal employees_pre_w
local dvar5 revenues_post_w seal revenues_pre_w
local dvar6 exit3 seal

* controls for polinomyal orders on both sides of the threshold
local linear    c.sealrank#i.seal

* regression loops reproducing Table 7
cap erase $output/Table_7.txt
cap erase $output/Table_7.tex
forv d=1/6 {
    forv i=2/2 {
	         est clear
             eststo: qui reghdfe `dvar`d'' `linear'    if phase_smei==`i' & (award!=1 | seal==1) & sumseal==1, a(competition) cluster(competition)
			 eststo: qui reghdfe `dvar`d'' `linear'    if phase_smei==`i' & inrange(sealrank, -10,10) &  (award!=1 | seal==1) & sumseal==1, a(competition) cluster(competition)
			 *eststo: qui reghdfe `dvar`d'' `linear'    if phase_smei==`i' & inrange(sealrank, -5,5) &  (award!=1 | seal==1) & sumseal==1, a(competition) cluster(competition)
			 esttab est* using $output/Table_7.txt, append star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) keep(seal) stat(N r2 aic, fmt(0 2)) title(Title: SOE firms vs rest of losing firms `e(depvar)' (Phase `i')) mtitles(All 10 ) ///
			 coeflabels(seal "Seal") indicate( "Rank x Seal=*.seal#c.sealrank" ) varwidth(25) 
			 esttab est* using $output/Table_7.tex, append star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) keep(seal) stat(N r2 aic, fmt(0 2)) title(Title: SOE firms vs rest of losing firms `e(depvar)' (Phase `i')) mtitles(All 10 ) ///
			 coeflabels(seal "Seal") indicate( "Rank x Seal=*.seal#c.sealrank" ) varwidth(25) 
    }
}



